package com.zsj.graph.dfs;

import com.zsj.graph.Node;

import java.util.HashSet;
import java.util.Set;
import java.util.Stack;

/**
 * @author zsj
 * @version 1.0
 * @date 2024/6/2 15:38
 * @description 深度优先遍历
 */
public class Code01_DFS {
    public static void dfs(Node node) {
        if (node == null) {
            return;
        }
        Stack<Node> stack = new Stack<>();
        Set<Node> set = new HashSet<>();
        stack.add(node);
        set.add(node);
        while (!stack.isEmpty()) {
            Node pop = stack.pop();
            for (Node next : pop.nexts) {
                if (!set.contains(next)) {
                    stack.push(pop);
                    stack.push(next);
                    set.add(next);
                    //做一些什么 比如打印什么的
                    break;
                }
            }
        }
    }
}
